package com.xindaoapp.henghua.db.model;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;
import android.text.TextUtils;

import com.xindaoapp.henghua.db.DaoSession;
import com.xindaoapp.henghua.entry.Photos;
import com.xindaoapp.henghua.entry.ScanData;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

/**
 * 获取所有设备的扫描记录
 * @author chenghao
 *
 */
public class ScanDatasDao extends AbstractDao<ScanData, String> {
	public static final String TABLENAME = "scandatas";

	public ScanDatasDao(DaoConfig config) {
		super(config);
	}

	public ScanDatasDao(DaoConfig config, DaoSession daoSession) {
		super(config, daoSession);
	}

	/**
	 * Properties of entity Note.<br/>
	 * Can be used for QueryBuilder and for referencing column names.
	 */
	public static class Properties {
		public final static Property AssertId = new Property(0, String.class, "assert_id", false, "assert_id");
		public final static Property Id = new Property(1, String.class, "id", false, "id", false);
		public final static Property TagCode = new Property(2, String.class, "tag_code", false, "tag_code",
				false);
		public final static Property ScanTime = new Property(3, String.class, "scan_time", false,
				"scan_time", false);
		public final static Property ScanLocationGps = new Property(4, String.class, "scan_location_gps",
				false, "scan_location_gps", false);
		public final static Property ScanLocation = new Property(5, String.class, "scan_location", false,
				"scan_location", false);
		public final static Property ScanDevice = new Property(6, String.class, "scan_device", false,
				"scan_device", false);
		public final static Property Status = new Property(7, String.class, "status", false, "status", false);
		public final static Property BreviaryPhotos = new Property(8, String.class, "breviaryphotos", false,
				"breviaryphotos", false);
		public final static Property PhotPhotos = new Property(9, String.class, "photphotos", false,
				"photphotos", false);
		public final static Property IsModified = new Property(10, Integer.class, "is_modified", false,
				"is_modified", false);
		public final static Property ScanUserId = new Property(11, String.class, "scan_userid", false,
				"scan_userid", false);
		public final static Property ScanUserName = new Property(12, String.class, "scan_username", false,
				"scan_username", false);
		public final static Property Notes = new Property(13, String.class, "notes", false, "notes", false);
		/** 大图文件路径 */
		public final static Property PhotosFilePath = new Property(14, String.class, "photos_file_path",
				false, "photos_file_path", false);
		/** 小图文件路径 */
		public final static Property BreviaryPhotosFilePath = new Property(15, String.class,
				"breviary_photos_file_path", false, "breviary_photos_file_path", false);
		/** 小图文件路径 */
		public final static Property ScanDeviceName = new Property(16, String.class, "scan_devicename",
				false, "scan_devicename", false);
	};

	/** Creates the underlying database table. */
	public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
		String constraint = ifNotExists ? "IF NOT EXISTS " : "";
		db.execSQL(String.format(
				"CREATE TABLE %s '%s' (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);",
				constraint, TABLENAME, createSqlCreate(Properties.AssertId), createSqlCreate(Properties.Id),
				createSqlCreate(Properties.TagCode), createSqlCreate(Properties.ScanTime),
				createSqlCreate(Properties.ScanLocationGps), createSqlCreate(Properties.ScanLocation),
				createSqlCreate(Properties.ScanDevice), createSqlCreate(Properties.Status),
				createSqlCreate(Properties.BreviaryPhotos), createSqlCreate(Properties.PhotPhotos),
				createSqlCreate(Properties.IsModified), createSqlCreate(Properties.ScanUserId),
				createSqlCreate(Properties.ScanUserName), createSqlCreate(Properties.Notes),
				createSqlCreate(Properties.PhotosFilePath),
				createSqlCreate(Properties.BreviaryPhotosFilePath),
				createSqlCreate(Properties.ScanDeviceName)));
	}

	/** Drops the underlying database table. */
	public static void dropTable(SQLiteDatabase db, boolean ifExists) {
		String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'" + TABLENAME + "'";
		db.execSQL(sql);
	}

	@Override
	protected ScanData readEntity(Cursor cursor, int offset) {
		ScanData entity = new ScanData();
		entity.assert_id = cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
		entity.id = cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1);
		entity.tag_code = cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2);
		entity.scan_time = cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3);
		entity.scan_location_gps = cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4);
		entity.scan_location = cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5);
		entity.scan_device = cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6);
		entity.status = cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7);
		String temp = cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8);
		Photos tempPhotos = new Photos();
		if (!TextUtils.isEmpty(temp)) {
//			tempPhotos = new Photos();
			String[] t = temp.split(",");
			tempPhotos.breviaryphotos = new ArrayList<String>();
			for (String str : t) {
				tempPhotos.breviaryphotos.add(str);
			}
		}
		temp = cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9);
		if (!TextUtils.isEmpty(temp)) {
//			if (null == tempPhotos) {
//				tempPhotos = new Photos();
//			}
			String[] t = temp.split(",");
			tempPhotos.photphotos = new ArrayList<String>();
			for (String str : t) {
				tempPhotos.photphotos.add(str);
			}
		}
		entity.photos = tempPhotos;
		entity.isModified = cursor.isNull(offset + 10) ? null : cursor.getInt(offset + 10);
		entity.scan_userid = cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11);
		entity.scan_username = cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12);
		entity.notes = cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13);
		entity.photoFilePath = cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14);
		entity.breviaryFilePath = cursor.isNull(offset + 15) ? null : cursor.getString(offset + 15);
		entity.scan_devicename = cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16);
		return entity;
	}

	@Override
	protected String readKey(Cursor cursor, int offset) {
//		return cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2);
		return null;
	}

	@Override
	protected void readEntity(Cursor cursor, ScanData entity, int offset) {
		entity.assert_id = cursor.isNull(offset + 0) ? null : cursor.getString(offset + 0);
		entity.id = cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1);
		entity.tag_code = cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2);
		entity.scan_time = cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3);
		entity.scan_location_gps = cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4);
		entity.scan_location = cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5);
		entity.scan_device = cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6);
		entity.status = cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7);
		Photos tempPhotos = new Photos();
		String temp = cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8);
		if (!TextUtils.isEmpty(temp)) {
//			tempPhotos = new Photos();
			String[] t = temp.split(",");
			tempPhotos.breviaryphotos = new ArrayList<String>();
			for (String str : t) {
				tempPhotos.breviaryphotos.add(str);
			}
		}
		temp = cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9);
		if (!TextUtils.isEmpty(temp)) {
//			if (null == tempPhotos) {
//				tempPhotos = new Photos();
//			}
			String[] t = temp.split(",");
			tempPhotos.photphotos = new ArrayList<String>();
			for (String str : t) {
				tempPhotos.photphotos.add(str);
			}
		}
		entity.photos = tempPhotos;
		entity.isModified = cursor.isNull(offset + 10) ? null : cursor.getInt(offset + 10);
		entity.scan_userid = cursor.isNull(offset + 11) ? null : cursor.getString(offset + 11);
		entity.scan_username = cursor.isNull(offset + 12) ? null : cursor.getString(offset + 12);
		entity.notes = cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13);
		entity.photoFilePath = cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14);
		entity.breviaryFilePath = cursor.isNull(offset + 15) ? null : cursor.getString(offset + 15);
		entity.scan_devicename = cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16);
	}

	@Override
	protected void bindValues(SQLiteStatement stmt, ScanData entity) {
		stmt.clearBindings();

		stmt.bindString(1, entity.assert_id);
		stmt.bindString(2, entity.id);
		stmt.bindString(3, entity.tag_code);
		stmt.bindString(4, entity.scan_time);
		stmt.bindString(5, entity.scan_location_gps);
		stmt.bindString(6, entity.scan_location);
		stmt.bindString(7, entity.scan_device);
		stmt.bindString(8, entity.status);
		StringBuffer sb = new StringBuffer();
		if (entity.photos != null && entity.photos.breviaryphotos != null) {
			for (String str : entity.photos.breviaryphotos) {
				sb.append(str);
				sb.append(",");
			}
			stmt.bindString(9, sb.toString());
		}
		sb = new StringBuffer();
		if (entity.photos != null && entity.photos.photphotos != null) {
			for (String str : entity.photos.photphotos) {
				sb.append(str);
				sb.append(",");
			}
			stmt.bindString(10, sb.toString());
		}
		stmt.bindLong(11, entity.isModified);
		stmt.bindString(12, entity.scan_userid);
		stmt.bindString(13, entity.scan_username);
		stmt.bindString(14, entity.notes);
		stmt.bindString(15, entity.photoFilePath);
		stmt.bindString(16, entity.breviaryFilePath);
		stmt.bindString(17, entity.scan_devicename);
	}

	@Override
	protected String updateKeyAfterInsert(ScanData entity, long rowId) {
		return entity.tag_code;
	}

	@Override
	protected String getKey(ScanData entity) {
		if (entity != null) {
			return entity.tag_code;
		} else {
			return null;
		}
	}

	@Override
	protected boolean isEntityUpdateable() {
		return true;
	}

}
